package com.example.prison.controller;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.prison.config.Response;
import com.example.prison.entity.AlarmRecord;
import com.example.prison.entity.PushManagement;
import com.example.prison.entity.PushRecord;
import com.example.prison.service.AlarmRecordService;
import com.example.prison.service.PushManagementService;
import com.example.prison.service.PushRecordService;
import com.example.prison.service.SystemLogService;
import com.example.prison.service.impl.PushRecordServiceImpl;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@Api("报警记录")
@RestController
@RequestMapping("/api")
public class AlarmRecordController {

    @Autowired
    private AlarmRecordService alarmRecordService;

    @Autowired
    private PushManagementService pushManagementService;

    @Autowired
    private PushRecordService pushRecordService;

    @Autowired
    private SystemLogService systemLogService;

    @ApiOperation("获取报警记录列表")
    @GetMapping("/getAlarmRecord")
    public Response<PageInfo<AlarmRecord>> getAlarm(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        log.info("获取报警记录列表");
        PageInfo<AlarmRecord> pageInfo = alarmRecordService.get(pageNumber, pageSize);
        if(ObjectUtils.isEmpty(pageInfo.getList())) {
            return Response.success("获取成功！", pageInfo);
        }
        return Response.success("获取成功！", pageInfo);
    }

    @ApiOperation("新增报警记录")
    @PostMapping("/addAlarmRecord")
    public Response<?> addAlarm(@RequestBody AlarmRecord record) {
        log.info("新增报警记录");
        systemLogService.add("新增报警记录");
        int result = alarmRecordService.add(record);
        if(result == -1) {
            return Response.error("新增失败！报警记录已存在且未处理");
        }
        return Response.success("添加成功！");
    }

    @ApiOperation("删除报警记录")
    @PostMapping("/deleteAlarmRecord")
    public Response<?> deleteAlarm(@Parameter(description = "报警记录ID") @RequestBody Integer[] ids) {
        log.info("删除报警记录");
        systemLogService.add("删除报警记录");
        alarmRecordService.delete(ids);
        return Response.success("删除成功！");
    }

    @ApiOperation("立即处理报警")
    @GetMapping("/handleAlarmRecordById")
    public Response<?> handleAlarmRecordById(@Parameter(description = "报警记录ID")@RequestParam(value = "id") Integer id,
                                             @Parameter(description = "当前处理人")@RequestParam(value = "processPeople") String processPeople,
                                             @Parameter(description = "处理时间")@RequestParam(value = "processTime") String processTime) {
        log.info("处理报警");
        systemLogService.add("处理报警");
        int result = alarmRecordService.updateRecord(id, processPeople, processTime);
        if(result == -1) {
            return Response.error("此报警记录id不存在！");
        }
        AlarmRecord record = alarmRecordService.getById(id);
        if(ObjectUtils.isNotEmpty(pushManagementService.getOneByAlarmLevel(record.getLevel()))) {
            PushManagement pushManage = pushManagementService.getOneByAlarmLevel(record.getLevel());
            PushRecord push = new PushRecord();
            push.setAlarmLevel(record.getLevel());
            push.setPushPersonnel(pushManage.getPushPersonnel());
            if (ObjectUtils.isNotEmpty(pushManage.getNote())) {
                push.setNote(pushManage.getNote() + "===>已处理");
            }
            push.setPushTime(new SimpleDateFormat("yyyy/M/d HH:mm:ss").format(new Date()));
            pushRecordService.add(push);
        } else {
            return Response.error("处理失败！推送设置中未设置此类报警对应处理");
        }
        return Response.success("处理成功！", record);
    }

    @ApiOperation("推送报警")
    @GetMapping("/handlePushAlarmRecord")
    public Response<?> handlePushAlarmRecord(@Parameter(description = "报警记录ID")@RequestParam(value = "id") Integer id,
                                             @Parameter(description = "推送处理人员")@RequestParam(value = "processPeople") String processPeople) {
        log.info("推送报警");
        systemLogService.add("推送报警");
        int record = alarmRecordService.updatePushRecord(id, processPeople);
        if(record == -1) {
            return Response.error("此报警记录id不存在！");
        }
        return Response.success("处理成功！", record);
    }

    @ApiOperation("依据id获取报警记录列表")
    @GetMapping("/getAlarmRecordById")
    public Response<AlarmRecord> getAlarmById(@Parameter(description = "报警记录ID")@RequestParam(value = "id") Integer id) {
        log.info("依据id获取报警记录列表");
        systemLogService.add("依据id获取报警记录列表");
        AlarmRecord record = alarmRecordService.getById(id);
        if(ObjectUtils.isEmpty(record)) {
            return Response.error("此报警记录id不存在！");
        }
        return Response.success("获取成功！", record);
    }

    @ApiOperation("依据设备编号获取报警记录")
    @GetMapping("/getAlarmRecordByNumber")
    public Response<PageInfo<AlarmRecord>> getAlarmByNumber(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                      @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                      @Parameter(description = "设备编号")@RequestParam(value = "number") String number) {
        log.info("依据设备编号获取报警记录");
        systemLogService.add("依据设备编号获取报警记录");
        PageInfo<AlarmRecord> pageInfo = alarmRecordService.getByNumber(pageNumber, pageSize, number);
        if(ObjectUtils.isEmpty(pageInfo.getList())) {
            return Response.error("此报警记录不存在！");
        }
        return Response.success("获取成功！", pageInfo);
    }

    @ApiOperation("依据时间获取报警记录")
    @GetMapping("/getAlarmRecordByTime")
    public Response<PageInfo<AlarmRecord>> getAlarmRecordByTime(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                            @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                            @Parameter(description = "开始时间")@RequestParam(value = "beginTime") String beginTime,
                                                            @Parameter(description = "结束时间")@RequestParam(value = "endTime") String endTime) {
        log.info("依据时间获取报警记录");
        systemLogService.add("依据时间获取报警记录");
        if(beginTime.compareTo(endTime) > 0) {
            return Response.error("时间顺序错误，请重新输入！");
        }
        PageInfo<AlarmRecord> pageInfo = alarmRecordService.getByTime(pageNumber, pageSize, beginTime, endTime);
        if(ObjectUtils.isEmpty(pageInfo.getList())) {
            return Response.error("该时间内报警记录不存在！");
        }
        return Response.success("获取成功！", pageInfo);
    }

    @ApiOperation("依据报警等级获取报警记录")
    @GetMapping("/getAlarmRecordByLevel")
    public Response<PageInfo<AlarmRecord>> getAlarmByLevel(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                            @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                            @Parameter(description = "报警等级")@RequestParam(value = "level") String level) {
        log.info("依据报警等级获取报警记录");
        systemLogService.add("依据报警等级获取报警记录");
        PageInfo<AlarmRecord> pageInfo = alarmRecordService.getByLevel(pageNumber, pageSize, level);
        if(ObjectUtils.isEmpty(pageInfo.getList())) {
            return Response.error("此报警记录不存在！");
        }
        return Response.success("获取成功！", pageInfo);
    }

}
